package com.atguigu.stgy.common.converter;

import com.atguigu.stgy.model.base.BaseTypeEnum;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;

//@Component
public class BaseTypeEnumConverterFactory implements ConverterFactory<String, BaseTypeEnum> {

    @Override
    public <T extends BaseTypeEnum> Converter<String, T> getConverter(Class<T> targetType) {
        return new Converter<String, T>() {
            @Override
            public T convert(String source) {
                for (T typeEnum : targetType.getEnumConstants()) {
                    if(source.equals(typeEnum.getType().toString())){
                            return typeEnum;
                    }
                }
                throw new IllegalArgumentException("类型不存在，转换失败");
            }
        };
    }
}
